package com.xxwy.dependency.injection.method;

import com.xxwy.dependency.User;
import com.xxwy.dependency.UserHold;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author wy
 * @date 2020/4/26 15:54
 */
@Configuration
public class AnnotationDependencyMethodInjectionDemo {

  private UserHold userHold;
  private UserHold userHold2;

  @Autowired
  public void initUserHolder(UserHold userHold){
    this.userHold = userHold;
  }

  @Resource
  public void initUserHolder2(UserHold userHold){
    this.userHold2 = userHold;
  }

  @Bean
  public UserHold userHold(User user) {
    System.out.println("UserHold @bean实例化");
    return new UserHold(user);
  }

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(AnnotationDependencyMethodInjectionDemo.class);
    XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(context);
    int definitions = definitionReader.loadBeanDefinitions("classpath:/META-INF/dependency-lookup-context.xml");
    context.refresh();

    AnnotationDependencyMethodInjectionDemo bean = context.getBean(AnnotationDependencyMethodInjectionDemo.class);

    System.out.println(bean.userHold == bean.userHold2);

    context.close();
  }


}
